//引入组件库
var do_Page = sm("do_Page");
var do_App = sm("do_App");
var nf = sm("do_Notification");
var do_DataCache = sm("do_DataCache");
var communityUrl = require("url/community");
var userUtil = require("helper/user");
var communityUtil = require("helper/community");
var qqHttp = require("helper/http");
var helperOpen = require("helper/open");
var gridViewCircle = ui("gridViewCircle");
// 声明UI变量
var rootview = ui("$"); // $表示当前视图的根UI
var labelNotice = ui("labelNotice");
var labelNoticeUserNick = ui("labelNoticeUserNick");
var labelNoticeTime = ui("labelNoticeTime");
var aLayoutNotice = ui("aLayoutNotice");
var labelCommuntiyName = ui("labelCommuntiyName");
var segmentViewUsers = ui("segmentViewUsers");
var labelRowNum = ui("labelRowNum");
var labelFront = ui("labelFront");
var labelBehind = ui("labelBehind");
var aLayoutFront = ui("aLayoutFront");
var aLayoutBehind = ui("aLayoutBehind");
var aLayout_notice_detail=ui("aLayout_notice_detail");
var do_ALayout_8=ui("do_ALayout_8");
var aLayoutAllCircle=ui("aLayoutAllCircle");
var labelCircleCount=ui("labelCircleCount");
var aLayoutAllImgs=ui("aLayoutAllImgs");
var gridViewImgs=ui("gridViewImgs");
var aLayoutRoot=ui("aLayoutRoot");
var do_LinearLayout_1=ui("do_LinearLayout_1");
// 本页面-ajax业务请求url
var httpUrl = communityUrl.url.base.index;

// 在aLayoutRoot上动态添加子视图(用于等待数据装载的过程)
var type_id = "community_base";
var communityHeight=communityUtil.communityHeight();
ui("do_ALayout_root").height=communityHeight;
ui("scrollView").height=communityHeight;
ui("do_ALayout_root").redraw();
ui("scrollView").redraw();
// 刷新数据
function loadData() {
	var params = {
		'id' : communityUtil.communityVisitId()
	};
	qqHttp.post(httpUrl, params, function(data) {
		loadIndex(data);
	})
}
var listdataUsers = mm("do_ListData");
segmentViewUsers.bindItems(listdataUsers);
var listdataCircles = mm("do_ListData");
gridViewCircle.bindItems(listdataCircles);
var noticeId;
function loadIndex(data) {
	if (data.notice.content != null && data.notice.content != '') {
		do_ALayout_8.visible=true;
		noticeId=data.notice.id;
		labelNotice.text = data.notice.content;
	} else {
		do_ALayout_8.visible=false;
		labelNotice.text = "";
	}
	if (data.notice.userNick != null && data.notice.userNick != '') {
		labelNoticeUserNick.text = data.notice.userNick;
		labelNoticeUserNick.visible = true;
	} else {
		labelNoticeUserNick.visible = false;
	}
	if (data.notice.dayMinutesNewAt != null
			&& data.notice.dayMinutesNewAt != '') {
		labelNoticeTime.text = data.notice.dayMinutesNewAt;
	} else {
		labelNoticeTime.text = "";
	}
	labelCommuntiyName.text = data.communityBase.name + "排名";
	if (null != data.cInfoVo) {
		labelRowNum.text = data.cInfoVo.rowNum;
	}
	if (null != data.frontCBase) {// 前一名
		labelFront.text = "";
		aLayoutFront.tag = null;
		aLayoutBehind.tag = null;
		labelBehind.text = "";
		labelFront.text = "前一名：" + data.frontCBase.name;
		aLayoutFront.tag = data.frontCBase.id
		if (null != data.behindCBase) {
			labelBehind.text = "后一名：" + data.behindCBase.name;
			aLayoutBehind.tag = data.behindCBase.id
		}
	} else {
		labelFront.text = "";
		labelBehind.text = "";
		aLayoutFront.tag = null;
		aLayoutBehind.tag = null;
		if (null != data.behindCBase) {
			labelFront.text = "后一名：" + data.behindCBase.name;
			aLayoutFront.tag = data.behindCBase.id;
		}
	}
	//用户
	listdataUsers.removeAll();
	if(null!=data.userList&&data.userList.length>0){
		listdataUsers.addData(data.userList);
	}
	segmentViewUsers.refreshItems();
	//圈子
	listdataCircles.removeAll();
	labelCircleCount.text=data.circlePage.totalElements;
	if(null!=data.circlePage&&data.circlePage.content.length>0){
		listdataCircles.addData(data.circlePage.content);
	}
	gridViewCircle.refreshItems();
	//社区图片
	if(data.communityBase.hasImg){
		do_LinearLayout_1.visible=true;
		gridViewImgs.items=data.communityBase.imgItems;
	}else{
		gridViewImgs.items=data.communityBase.imgItems;
		do_LinearLayout_1.visible=false;
	}
	aLayoutRoot.redraw();
	do_LinearLayout_1.redraw();
	ui("scrollView").redraw();
	ui("scrollView").toBegin();
}
//点击社区圈子
aLayoutAllCircle.on("touch", function(data) {
	helperOpen.open("source://view/circle/base/list/list.ui",{communityId:communityUtil.communityVisitId()});
});

aLayout_notice_detail.on("touch", function() {
	if(null!=noticeId){
		helperOpen.open("source://view/community/notice/detail/main.ui",{id:noticeId});
	}
})

aLayoutFront.on("touch", function() {
	communityUtil.communityVisitId(this.tag);
	do_Page.fire("community_index_refash");
})
aLayoutBehind.on("touch", function() {
	communityUtil.communityVisitId(this.tag);
	do_Page.fire("community_index_refash");
})
function loadPage() {
	// 更新远程最新数据
	loadData();
}
// 在当前页面下订阅community_change_join的事件
/*do_Page.on("community_change_join", function(data) {
	loadPage();
});*/

do_Page.on("community_home_tab_touch", function(data) {
	loadPage();
});

aLayoutNotice.on("touch", function() {// 公告
	helperOpen.open("source://view/community/notice/main.ui");
});


